package com.moonciki.artadmin.core.utils.excel;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.InputStream;

/** 旧版本 **/
class HExcelReader extends ExcelReader{

    protected HExcelReader(InputStream input, int columnLength, int sheetNumber) throws Exception{
        this.input = input;
        this.columnLength = columnLength;
        this.sheetNumber = sheetNumber;

        workbook = new HSSFWorkbook(input);
        currentSheet = workbook.getSheetAt(sheetNumber);

        lastRowNum = currentSheet.getLastRowNum();

        formulaEvaluator =  workbook.getCreationHelper().createFormulaEvaluator();
    }

    @Override
    public Object[] readExcel(int rowNumber) {
        HSSFRow hssfRow = (HSSFRow)currentSheet.getRow(rowNumber);

        boolean isEmpty = true;

        Object[] valueArray = new Object[columnLength];

        if (hssfRow != null) {

            for(int i=0; i<columnLength; i++){

                //String colName = columnArray[i];

                HSSFCell cell = hssfRow.getCell(i);
                Object cellValue = null;
                if(cell != null){
                    cellValue = getValue(cell,i);
                }

                valueArray[i] = cellValue;

                if(cellValue != null && StringUtils.isNotBlank(cellValue.toString())){
                    isEmpty = false;
                }

            }
        }

        //如果这一行没有任何数据
        if(isEmpty){
            valueArray = null;
        }

        return valueArray;
    }

}
